//
//  MianViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-6.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MainViewController.h"

#import "HomeProgramaCollectionViewCell.h"
#import <SwipeView/SwipeView.h>
#import "ADBannerView.h"

#import "ProgramModel.h"
#import "ADModel.h"
#import "LoginModel.h"
#import "UserModel.h"
#import "HomeModel.h"
#import "ActivityModel.h"
#import "ADViewController.h"

@interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,ADBannerViewDelegate>
@property (nonatomic , weak) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UIButton *userNarButton;

@property (weak, nonatomic) IBOutlet ADBannerView *bannerView;

@property (nonatomic , strong) NSMutableArray *itemsData;
@property (nonatomic , strong) NSMutableArray *adItemsData;
@end

@implementation MainViewController

-(void)awakeFromNib
{
   

    
    
    [_itemsData addObjectsFromArray:@[@"home_button_1.png",
                                      @"home_button_2.png",
                                      @"home_button_3.png",
                                      @"home_button_4.png",
                                      @"home_button_5.png",
                                      @"home_button_6.png"]];
    
//    [_adItemsData addObjectsFromArray:@[@"http://h.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266876a8a1da918972bd4073671.jpg",
//                                     @"http://a.hiphotos.baidu.com/image/pic/item/472309f7905298220a8bcf9cd4ca7bcb0a46d431.jpg",
//                                     @"http://e.hiphotos.baidu.com/image/pic/item/64380cd7912397dd55c8d9055a82b2b7d0a2877a.jpg"]];
}

-(void)fetchHomeData{
    [[HomeModel sharedModel] fetchHomeStructure:^(BOOL success, NSArray *array, NSString *msg) {
        
    }];
}


-(void)fetchADList{
    [_adItemsData removeAllObjects];
    [[ADModel sharedModel] fetchADListWithChannel:ADChannel code:@"1" result:^(BOOL success, NSArray *array) {
        if (success) {
            NSMutableArray *imageArray = [NSMutableArray array];
            for (ADListItem *item in array) {
                [imageArray addObject:item.imgSmall];
            }
            
            [_adItemsData addObjectsFromArray:array];
            
            self.bannerView.listArray = [NSArray arrayWithArray:imageArray];
            imageArray = nil;
            self.bannerView.scorllAnimation = YES;
            [self.bannerView show];
        }
       
    }];
}


-(void)adBannerSelectedIndex:(NSInteger)index{
    NSLog(@"广告点击%d",index);
    ADListItem *adItem = _adItemsData[index];
    if ([adItem.type isEqualToString:@"url"]) {
        ADViewController *adView = [self.storyboard instantiateViewControllerWithIdentifier:@"ADViewController"];
        adView.item = adItem;
        [self.navigationController pushViewController:adView animated:YES];
        
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _itemsData = [NSMutableArray array];
    _adItemsData = [NSMutableArray array];
    NSUserDefaults *userLogin = [[NSUserDefaults alloc] init];
    if ([userLogin objectForKey:@"phone"] != nil && [userLogin objectForKey:@"pwd"] != nil) {
        [[LoginModel sharedModel] loginWithPhoneNum:[userLogin objectForKey:@"phone"] password:[userLogin objectForKey:@"pwd"] result:^(BOOL success, NSString *msg) {
            if (success) {
                [self updateUserHead];
            }
        }];
    }
    
    [_itemsData addObjectsFromArray:@[@"home_button_1.png",
                                      @"home_button_2.png",
                                      @"home_button_3.png",
                                      @"home_button_4.png",
                                      @"home_button_5.png",
                                      @"home_button_6.png"]];
    
//    [_adItemsData addObjectsFromArray:@[@"http://h.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266876a8a1da918972bd4073671.jpg",
//                                        @"http://a.hiphotos.baidu.com/image/pic/item/472309f7905298220a8bcf9cd4ca7bcb0a46d431.jpg",
//                                        @"http://e.hiphotos.baidu.com/image/pic/item/64380cd7912397dd55c8d9055a82b2b7d0a2877a.jpg"]];
    
    [self fetchHomeData];
    [self fetchADList];
    // Do any additional setup after loading the view.
}

-(void)updateUserHead{
    if ([LoginModel sharedModel].userID != nil) {
    
        
        self.userNarButton.imageView.layer.masksToBounds = YES;
        self.userNarButton.imageView.layer.cornerRadius = CGRectGetWidth(self.userNarButton.frame)/2;
        self.userNarButton.imageView.layer.borderColor  = [UIColor whiteColor].CGColor;
        self.userNarButton.imageView.layer.borderWidth = 1.f;
        [self.userNarButton.imageView sd_setImageWithURL:[NSURL URLWithString:[UserModel sharedModel].userInfo.headPic] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self.userNarButton setImage:image forState:UIControlStateNormal];
        }];
    }else{
        self.userNarButton.imageView.layer.masksToBounds = YES;
        self.userNarButton.imageView.layer.cornerRadius = 0.f;
        self.userNarButton.imageView.layer.borderColor  = [UIColor clearColor].CGColor;
        self.userNarButton.imageView.layer.borderWidth = 0.f;
        [self.userNarButton setImage:[UIImage imageNamed:@"navi_login_button"] forState:UIControlStateNormal];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
    [self updateUserHead];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}


#pragma mark - action
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return [_itemsData count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeProgramaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeProgramaCollectionViewCell" forIndexPath:indexPath];
    [cell.itemImageView setImage:[UIImage imageNamed:_itemsData[indexPath.row]]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *actions = @[@"_jumpToProgramIntroducedAction:",
                         @"_jumpToLiveAction:",
                         @"_jumpToStockAction:",
                         @"_jumpToComperesAction:",
                         @"_jumpToActivityAction:",
                         @"_jumpToAnswerAction:"];
    if (indexPath.row < actions.count) {
        NSString *selStr = actions[indexPath.row];
        if (selStr && selStr.length > 0) {
            SEL sel = NSSelectorFromString(selStr);
            [self performSelector:sel withObject:self];
        }
    }
}

- (IBAction)_jumpToMeAction:(id)sender {
    
    if ([LoginModel sharedModel].userID == nil) {
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        _Nav2Board(@"Me");
    }
    
}

- (IBAction)_jumpToSettingAction:(id)sender {
    
    _Nav2ViewControl(@"Me",@"MeSetting");
}


-(IBAction)_jumpToProgramIntroducedAction:(id)sender
{
    _Nav2ViewControl(@"ProgramIntroduced",@"ProgramIntroducedListViewController");
}

-(IBAction)_jumpToLiveAction:(id)sender
{
    _Nav2ViewControl(@"Live",@"LiveViewController");
}
- (IBAction)_jumpToStockAction:(id)sender {
    _Nav2ViewControl(@"Main", @"StockViewController");
}

- (IBAction)_jumpToActivityAction:(id)sender {
    if ([LoginModel sharedModel].userID == nil) {
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        _Nav2ViewControl(@"Activity", @"ActivityViewController");
    }
    
}

- (IBAction)_jumpToComperesAction:(id)sender {
    _Nav2ViewControl(@"Comperes", @"ComperesListViewController");
}

- (IBAction)_jumpToMyComperesAction:(id)sender {
    if ([LoginModel sharedModel].userID == nil) {
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        _Nav2ViewControl(@"MyComperes", @"MyComperesListViewController");
    }
    
}
- (IBAction)_jumpToAnswerAction:(id)sender {
    _Nav2ViewControl(@"Answer", @"AnswerListViewController");
}


//#pragma mark -SwipeViewDataSource
//- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
//{
//    return [_adItemsData count];
//}
//
//- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
//{
//    UIImageView *showImageView = (UIImageView *)view;
//    if (!showImageView) {
//        showImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_swipeView.bounds), CGRectGetHeight(_swipeView.bounds))];
//        
//        view = showImageView;
//    }
//    
//    [showImageView sd_setImageWithURL:_adItemsData[index] placeholderImage:nil];
//    
//    return view;
//}

//#pragma mark - SwipeViewDelegate
//- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
//{
//    
//}
//
//- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
//{
//    
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
